package com.library.web.controller.library;


import com.library.common.annotation.Log;
import com.library.common.core.controller.BaseController;
import com.library.common.core.domain.AjaxResult;
import com.library.common.core.page.TableDataInfo;
import com.library.common.core.text.Convert;
import com.library.common.enums.BusinessType;
import com.library.common.utils.file.FileUploadUtils;
import com.library.common.utils.poi.ExcelUtil;
import com.library.domain.MbGift;
import com.library.framework.config.CosConfig;
import com.library.framework.manager.CosUploadManager;
import com.library.service.IMbGiftService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Objects;


/**
 * 签到礼品 信息操作处理
 *
 * @author null
 * @date 2020-04-23
 * @blame Android Team
 */
@RestController
@RequestMapping("/mobile/mbGift")
@SuppressWarnings("all")
public class MbGiftController extends BaseController {
    private String prefix = "business/mobile/mbGift";
    private String cosPrefix = "/gift/";

    @Autowired
    private IMbGiftService mbGiftService;

    @Autowired
    private CosConfig cosConfig;


    @GetMapping()
    public String mbGift() {
        return prefix + "/mbGift";
    }

    /**
     * 查询签到礼品列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MbGift mbGift) {
        startPage();
        List<MbGift> list = mbGiftService.selectMbGiftList(mbGift);
        return getDataTable(list);
    }


    /**
     * 导出签到礼品列表
     */
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MbGift mbGift) {
        List<MbGift> list = mbGiftService.selectMbGiftList(mbGift);
        ExcelUtil<MbGift> util = new ExcelUtil<MbGift>(MbGift.class);
        return util.exportExcel(list, "mbGift");
    }

    /**
     * 新增签到礼品
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存签到礼品
     */
    @Log(title = "签到礼品", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam(value = "simpleImage", required = false) MultipartFile simpleImageFile,
                              @RequestParam(value = "detailsImage", required = false) MultipartFile detailsImageFile,
                              MbGift mbGift) throws IOException {

        mbGift.setGiftStatus("0");
        mbGift.setConvertNum(0);
        mbGift.setNoConvertNum(0);
        return toAjax(mbGiftService.insertMbGift(mbGift));
    }

    /**
     * 修改签到礼品
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        MbGift mbGift = mbGiftService.selectMbGiftById(id);
        mmap.put("mbGift", mbGift);
        return prefix + "/edit";
    }

    /**
     * 修改保存签到礼品
     */
    @Log(title = "签到礼品", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestParam(value = "simpleImage", required = false) MultipartFile simpleImageFile,
                               @RequestParam(value = "detailsImage", required = false) MultipartFile detailsImageFile,
                               MbGift mbGift) throws IOException {

        mbGift.setGiftStatus(statusToChar(mbGift.getGiftStatus()));
        return toAjax(mbGiftService.updateMbGift(mbGift));
    }

    /**
     * 删除签到礼品
     */
    @Log(title = "签到礼品", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        String[] strings = Convert.toStrArray(ids);
        for (String id : strings) {
            MbGift mbGift = mbGiftService.selectMbGiftById(Integer.valueOf(id));
            Integer noConvertNum = mbGift.getNoConvertNum();
            if (noConvertNum > 0) {
                return AjaxResult.error("存在未兑换商品[" + mbGift.getGiftName() + "]不能删除");
            }
        }
        return toAjax(mbGiftService.deleteMbGiftByIds(ids));
    }

}
